/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.view;

import de.uhilger.app.ui.GridBagHelper;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

/**
 * <p>A user interface component to change a password.</p>
 *
 * <p>This component is unfinished for the time being as there is meant for changes to the
 * password of a user who owns a Derby database. As long as there is no technical way to change
 * that password it does not reeally make sense.</p>
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class ChangePasswordDialog extends JDialog implements ActionListener {

  private static final String RN_CHG_PW_PANEL_CURRENT_PW_LABEL = "chgPwPanelCurrentPwLabel";
  private static final String RN_CHG_PW_PANEL_NEW_PW_LABEL = "chgPwPanelNewPwLabel";
  private static final String RN_CHG_PW_PANEL_REPEAT_PW_LABEL = "chgPwPanelRepeatPwLabel";
  private static final String RN_CHG_PW_PANEL_CANCEL_BTN_LABEL = "chgPwPanelCancelBtnLabel";
  private static final String RN_CHG_PW_PANEL_OK_BTN_LABEL = "chgPwPanelOkBtnLabel";

  private JPasswordField currentPw;
  private JPasswordField newPw;
  private JPasswordField repeatPw;
  private JButton cancelBtn;
  private JButton okBtn;

  public ChangePasswordDialog(ResourceBundle res) {
    super();
    buildUi(res);
  }

  private void buildUi(ResourceBundle res) {
    GridBagHelper gbh = new GridBagHelper();
    GridBagConstraints c = new GridBagConstraints();
    GridBagLayout g = new GridBagLayout();

    JPanel panel = new JPanel(g);

    // line 0

    JLabel label = new JLabel(res.getString(RN_CHG_PW_PANEL_CURRENT_PW_LABEL));
    gbh.addGridBagComponent(panel, label, g, c, 0, 0, GridBagConstraints.EAST);

    currentPw = new JPasswordField();
    gbh.addGridBagComponent(panel, currentPw, g, c, 1, 0, GridBagConstraints.WEST);

    // line 1

    label = new JLabel(res.getString(RN_CHG_PW_PANEL_NEW_PW_LABEL));
    gbh.addGridBagComponent(panel, label, g, c, 0, 1, GridBagConstraints.EAST);

    newPw = new JPasswordField();
    gbh.addGridBagComponent(panel, newPw, g, c, 1, 1, GridBagConstraints.WEST);

    // line 2

    label = new JLabel(res.getString(RN_CHG_PW_PANEL_REPEAT_PW_LABEL));
    gbh.addGridBagComponent(panel, label, g, c, 0, 2, GridBagConstraints.EAST);

    repeatPw = new JPasswordField();
    gbh.addGridBagComponent(panel, repeatPw, g, c, 1, 2, GridBagConstraints.WEST);

    // line 3
    
    cancelBtn = new JButton(res.getString(RN_CHG_PW_PANEL_CANCEL_BTN_LABEL));
    cancelBtn.addActionListener(this);
    okBtn = new JButton(res.getString(RN_CHG_PW_PANEL_OK_BTN_LABEL));
    okBtn.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okBtn);
    buttonPanel.add(cancelBtn);
    gbh.addGridBagComponent(panel, buttonPanel, g, c, 1, 3, GridBagConstraints.EAST);

    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(panel, BorderLayout.CENTER);
  }
  
  private boolean inputOk() {
    boolean inputIsOk = false;
    return inputIsOk;
  }

  public String getCurrentPassword() throws Exception {
    String curPw = null;
    if(inputOk()) {
      curPw = new String(currentPw.getPassword());
    } else {
      throw new Exception("Typo in new password");
    }
    return curPw;
  }

  public String getNewPassword() throws Exception {
    return new String(newPw.getPassword());
  }

  public void actionPerformed(ActionEvent e) {
    Object src = e.getSource();
    if(src != null) {
      if(src.equals(cancelBtn)) {
        throw new UnsupportedOperationException("Not supported yet.");
      } else if(src.equals(okBtn)) {
        throw new UnsupportedOperationException("Not supported yet.");
      }
    }
  }

}
